<?php

$size = 15;

// 生成页码
$range = $total_pages > 1 ? range(1, $total_pages) : array(1);
$rangeSize = count($range);

// 预置参考偏移量
$minOffset = 0;
$maxOffset = max(array(0, $rangeSize - $size));

// 计算偏移量
$offset = $current_page - floor($size / 2);

// 校正偏移量
if ($offset < $minOffset) {
    $offset = $minOffset;
}

if ($offset > $maxOffset) {
    $offset = $maxOffset;
}

// 切割页码
$range = array_slice($range, $offset, $size);
?>

<div class="manu">
    <?php if ($first_page !== FALSE): ?>
        <a href="<?php echo $page->url($first_page) ?>">首页</a>
    <?php else: ?>
        <span class="disabled">首页</span>
    <?php endif ?>

    <?php if ($previous_page !== FALSE): ?>
        <a href="<?php echo $page->url($previous_page) ?>">上一页</a>
    <?php else: ?>
        <span class="disabled">上一页</span>
    <?php endif ?>

    <?php foreach ($range as $i): ?>

        <?php if ($i == $current_page): ?>
            <span class="current"><?php echo $i ?></span>
        <?php else: ?>
            <a href="<?php echo $page->url($i) ?>"><?php echo $i ?></a>
        <?php endif ?>

    <?php endforeach ?>

    <?php if ($next_page !== FALSE): ?>
        <a href="<?php echo $page->url($next_page) ?>">下一页</a>
    <?php else: ?>
        <span class="disabled">下一页</span>
    <?php endif ?>

    <?php if ($last_page !== FALSE): ?>
        <a href="<?php echo $page->url($last_page) ?>">末页</a>
    <?php else: ?>
        <span class="disabled">末页</span>
    <?php endif ?>

    <?php echo "<strong>共{$total_pages}<strong>页"?>
</div>